/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import domain.Contract;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
/**
 *
 * @author Alejandro
 */
public class ContractDao {
    EntityManager em;
    EntityTransaction tx;

    public ContractDao() {
        em = SingletonDao.getInstance().getEntityManager();
        tx = SingletonDao.getTx();
    }

    public Contract getContract(Integer idContract) {
        Contract w = (Contract) em.createNativeQuery("select * from contract as n where n.idContract='" + idContract + "'", Contract.class).getSingleResult();
        return w;
    }

    public void saveContract(Contract w) {
        tx.begin();
        em.persist(w);
        tx.commit();
    }

    public void removeContract(Contract w) {
        tx.begin();
        em.remove(w);
        tx.commit();
    }

    public void updateContract(Contract c) {
        tx.begin();
        em.createNativeQuery("update contract set dateContract='"+c.getDateContract()+"', bankFK='"+c.getBankaccount().getIdBank()+"', nameContract='"+c.getNameContract()+"' where idContract='"+c.getIdContract()+"'").executeUpdate();
        tx.commit();
    }

}
